<?php
    defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @property  admin_model admin_model
*/
class Admin extends Admin_Controller
    {

        /**
         * Index Page for this controller.
         *
         * Maps to the following URL
         *        http://example.com/index.php/welcome
         *    - or -
         *        http://example.com/index.php/welcome/index
         *    - or -
         * Since this controller is set as the default controller in
         * config/routes.php, it's displayed at http://example.com/
         *
         * So any other public methods not prefixed with an underscore will
         * map to /index.php/welcome/<method_name>
         *
         * @see https://codeigniter.com/user_guide/general/urls.html
         */
        public function index()
        {
            $this->load->view('admin_message');
        }

        /**
         * 修改密码
         * author: walker
         * Date: 2019/11/7
         * Time: 11:35
         * Note:
         */
        public function update_pass()
        {
            if ($_POST){

                $this->load->library('session');
                $this->load->library('sys_message');

                $this->load->model('admin_model');

                $old_pass = md5(htmlspecialchars(trim($this->input->post('old_pass'))));
                $new_pass = htmlspecialchars(trim($this->input->post('new_pass')));
                $con_pass = htmlspecialchars(trim($this->input->post('con_pass')));

                $new_pass != $con_pass && $this->sys_message->warning('两次输入密码不一致！');

                $user = $this->admin_model->validation_user($this->session->admin_name, $old_pass);

                !$user && $this->sys_message->warning('旧密码错误！');

                $this->admin_model->update_pass($new_pass, $old_pass);

                $this->sys_message->success('修改成功！');
            }

            $this->load->view('update_pass');
        }


        /**
         * 授权管理展示
         * author: walker
         * Date: 2019/11/7
         * Time: 11:34
         * Note:
         */
        public function admin_power()
        {
            $this->load->model('admin_model');
            if ($_POST){
                $data = $this->input->post();
                $arr  = $this->admin_model->authorization($data);
                redirect('Admin/admin_power');
            }
            $res = $this->admin_model->method_list(1);
            foreach ($res as $k => $v) {
                if ($v['p_id'] == 0){
                    $controller[] = $v;
                } else {
                    $method[] = $v;
                }
            }
            foreach ($controller as $key => $val) {
                foreach ($method as $k => $v) {
                    if ($v['p_id'] == $val['id']){
                        // $v['len'] = '------';
                        // $controller_method[] = $v;
                        $controller[$key]['son'][] = $v;
                    }
                }
            }
            $admin_list = $this->admin_model->admin_option();
            $data       = array(
                'admin_list' => $admin_list,
                'controller' => $controller,
            );
            $this->load->view('admin/admin_power', $data);
        }


        //方法控制器录入

        /**
         * 方法控制器录入
         * author: walker
         * Date: 2019/11/7
         * Time: 11:35
         * Note:
         */
        public function method_list()
        {
            $this->load->model('admin_model');
            $res = $this->admin_model->method_list();
            foreach ($res as $k => $v) {
                if ($v['p_id'] == 0){
                    $controller[] = $v;
                } else {
                    $method[] = $v;
                }
            }
            foreach ($controller as $key => $val) {
                $controller_method[] = $val;
                foreach ($method as $k => $v) {
                    if ($v['p_id'] == $val['id']){
                        $controller_method[] = $v;
                    }
                }
            }
            $data = array(
                'res' => $controller_method,
            );
            $this->load->view('admin/method_list', $data);

        }

        //修改展示不展示
        public function is_show()
        {
            $this->load->model('admin_model');
            $id = $this->input->get('id');
            $this->admin_model->is_show($id);
            redirect('Admin/method_list');
        }

        //添加控制器方法
        public function method_add()
        {
            $this->load->model('admin_model');
            if ($_POST){
                $data = [
                    'controller'   => $this->input->post('controller'),
                    'method'       => $this->input->post('method'),
                    'describe'     => $this->input->post('describe'),
                    'is_show'      => $this->input->post('is_show'),
                    'is_parameter' => $this->input->post('is_parameter'),
                    'p_id'         => $this->input->post('p_id'),
                    'add_time'     => time(),
                ];
                $res  = $this->admin_model->method_add($data);
                redirect('Admin/method_list');
            }
            $res = $this->admin_model->method_list(1);
            foreach ($res as $k => $v) {
                if ($v['p_id'] == 0){
                    $controller[] = $v;
                } else {
                    $method[] = $v;
                }
            }
            foreach ($controller as $key => $val) {
                $controller_method[] = $val;
                foreach ($method as $k => $v) {
                    if ($v['p_id'] == $val['id']){
                        $v['len']            = '------';
                        $controller_method[] = $v;
                    }
                }
            }
            $data = array(
                'res' => $controller_method,
            );
            $this->load->view('admin/method_add', $data);
        }

        //控制器方法修改
        public function method_save()
        {
            $this->load->model('admin_model');
            if ($_POST){
                $data = array(
                    'controller'   => $this->input->post('controller'),
                    'method'       => $this->input->post('method'),
                    'describe'     => $this->input->post('describe'),
                    'is_show'      => $this->input->post('is_show'),
                    'is_parameter' => $this->input->post('is_parameter'),
                    'p_id'         => $this->input->post('p_id'),
                    'id'           => $this->input->post('id'),
                );
                $this->admin_model->method_save($data);
                redirect('Admin/method_list');
            }
            $id   = $this->input->get('id');
            $data = $this->admin_model->method_find($id);
            $res  = $this->admin_model->method_list(1);
            foreach ($res as $k => $v) {
                if ($v['p_id'] == 0){
                    $controller[] = $v;
                } else {
                    $method[] = $v;
                }
            }
            foreach ($controller as $key => $val) {
                $controller_method[] = $val;
                foreach ($method as $k => $v) {
                    if ($v['p_id'] == $val['id']){
                        $v['len']            = '------';
                        $controller_method[] = $v;
                    }
                }
            }
            $data = array(
                'res'  => $controller_method,
                'data' => $data,
            );
            $this->load->view('admin/method_save', $data);
        }

        /**
         * 空值检测
         *
         * @param array $data
         * @param array $field
         * author: walker
         * Date: 2019/11/7
         * Time: 14:12
         * Note:
         */
        public function check_field(array $data, array $field)
        {
            foreach ($field as $key => $val) {
                if (empty($data[$key])){
                    ajaxReturn(4001, $val . '不能为空');
                }
            }
        }

        /**
         * 添加部门
         * author: walker
         * Date: 2019/11/7
         * Time: 11:36
         * Note:
         */
        public function department_add()
        {
            $this->load->model('admin_model');
            $model = new Admin_model();
            if ($_POST){

                $pid  = $this->input->post('pid');
                $data = array(
                    'department_name' => $this->input->post('department_name'),
                    'desc'            => $this->input->post('desc'),
                    'status'          => $this->input->post('status'),
                    'pid'             => $pid,
                    'add_time'        => time(),
                    'level'           => 1,
                );
                $id   = $_POST['id'];
                if ( !empty($id)){
                    $data['id'] = $_POST['id'];
                }
                /**
                 * 检查字段
                 */
                $this->check_field($data, array('department_name' => '部门名称', 'status' => '启用'));
                $department_info = $model->department_get_id($data['department_name']);
                if ( !empty($pid)){
                    $parentLevel   = $model->get_parent_level($pid)['level'];
                    $data['level'] = $parentLevel + 1;
                }
                if ($id){

                    $res = $model->update_department($id, $data);
                } else {
                    if ( !empty($department_info)){
                        ajaxReturn(4002, '该部门已经存在');
                    }
                    $res = $model->department_add($data);
                }
                if ($res){
                    ajaxReturn(200, '添加成功', array('url' => 'department_list'));
                }
            }
            $id   = $_GET['id'];
            $info = array();
            if ( !empty($id)){
                $info = $this->admin_model->get_department_by_id($id);
            }
            $cate = $this->admin_model->get_department_list();
            /**
             * 剔除自己的分类
             */
            if ($id){
                foreach ($cate as $key => $val) {
                    if ($val['id'] == $id){
                        unset($cate[$key]);
                    }
                }
            }
            $data         = array();
            $data['info'] = $info[0];
            $data['cate'] = $cate;
            $this->load->view('admin/department_add', $data);
        }

        /**
         * 部门列表
         * author: walker
         * Date: 2019/11/7
         * Time: 15:41
         * Note:
         */
        public function department_list()
        {
            $model = get_model('department_model');
            $list = $model->j_list(['is_del' => 0]);
            $this->load->view('admin/department_list', array('list' => $list));
        }

        /**
         * 删除部门
         * author: walker
         * Date: 2019/11/7
         * Time: 15:41
         * Note:
         */
        public function sync_edit_department()
        {
            $this->load->model('admin_model');
            $params = $_POST;
            $id     = $params['id'];
            $act    = $params['act'];
            if (empty($id)){
                ajaxReturn(4001, '缺少必要参数');
            }
            $info = $this->admin_model->get_department_by_id($id);
            if (empty($info)){
                ajaxReturn(4002, '该消息不存在');
            }
            /**
             * 软删除
             */
            switch ($act) {
                case 'del':
                    $res = $this->admin_model->del_department($id);
                    break;
                case 'status':
                    $res = $this->admin_model->edit_department_status($id);
                    break;
            }
            if ($res){
                ajaxReturn(200, '成功');
            }
            ajaxReturn(4002, '失败');
        }


        /**
         * 添加修改角色
         * author: walker
         * Date: 2019/11/7
         * Time: 11:36
         * Note:
         */
        public function role_from()
        {
            $this->load->model('admin_model');
            $model = new Admin_model();
            if ($_POST){

                $data = array(
                    'role_name' => $this->input->post('role_name'),
                    'status'    => $this->input->post('status'),
                    'permission'    => implode(',', $this->input->post('permission'))
                );

                $id = $_POST['id'];
                if ( !empty($id)){
                    $data['id'] = $_POST['id'];
                }
                /**
                 * 检查字段
                 */
                $this->check_field($data, array('role_name' => '角色名称', 'status' => '启用'));
                $role_info = $model->get_role_id($data['role_name']);
                if ($id){
                    $data['update_time'] = time();
                    $res                 = $model->update_role($id, $data);
                } else {
                    if ( !empty($role_info)){
                        ajaxReturn(4002, '该部门已经存在');
                    }
                    $data['add_time'] = time();
                    $res              = $model->add_role($data);
                }
                if ($res){
                    ajaxReturn(200, '添加成功', array('url' => 'department_list'));
                }
            }
            $id   = $_GET['id'];

            $role_model = get_model('role_model');

            $info = array();
            if ( !empty($id)){
                $info = $role_model->j_row(['id' => $id]);
            }
            $data         = array();

            /** @var menu $menu_lib */
            $menu_lib = get_lib('menu');
            $menu = $menu_lib->get_all_tree_menu();
            $data['menu'] = $menu;

            $info['permission'] = explode(',', $info['permission']);

            $data['info'] = $info;
            $this->load->view('admin/add_role', $data);
        }

        /**
         * 角色列表
         * author: walker
         * Date: 2019/11/7
         * Time: 15:41
         * Note:
         */
        public function role_list()
        {
            $this->load->model('admin_model');
            $list = $this->admin_model->get_role_list();

            $this->load->view('admin/list_role', array('list' => $list));
        }

        /**
         * 删除角色
         * author: walker
         * Date: 2019/11/7
         * Time: 15:41
         * Note:
         */
        public function sync_edit_role()
        {
            $this->load->model('admin_model');
            $params = $_POST;
            $id     = $params['id'];
            $act    = $params['act'];
            if (empty($id)){
                ajaxReturn(4001, '缺少必要参数');
            }
            $info = $this->admin_model->get_role_by_id($id);

            if (empty($info)){
                ajaxReturn(4002, '该消息不存在');
            }
            /**
             * 软删除
             */
            switch ($act) {
                case 'del':
                    $res = $this->admin_model->del_role($id);
                    break;
                case 'status':
                    $res = $this->admin_model->edit_role_status($id);
                    break;
            }
            if ($res){
                ajaxReturn(200, '成功');
            }
            ajaxReturn(4002, '失败');
        }

        /**
         * 用户列表
         * author: walker
         * Date: 2019/11/8
         * Time: 10:48
         * Note:
         */
        public function user_list()
        {
            $this->load->model('admin_model');
            $data             = array();

            $data['title']    = '用户';
            $data['sync_url'] = 'sync_edit_user';

            $_param_option = array(
                'admin_name' => array(
                    'col' => 'col-lg-4',
                    'type' => 'text',
                    'placeholder' => '输入用户名称'
                )
            );
            $data['param_option']  = $_param_option;

            $_action_link = array(array('href' => base_url('Admin/user_from'), 'name' => '添加用户'));
            $data['action_link']  = $_action_link;

            $_param = array('is_del' => 0);

            if($this->input->get('admin_name')){
                $_param['admin_name|like'] = $this->input->get('admin_name');
            }

            $assoc = $this->uri->uri_to_assoc();
            $page = $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0;
            $limit = $this->admin_model->limit_rows;

            $list             = $this->admin_model->j_list($_param, '*', $page * $limit, $limit, 'admin_id desc');
            $data['list']     = $list;

            $this->load->library('pagination');
            $config['base_url'] = base_url('Admin/') . 'user_list/page/';
            $config['total_rows'] = $this->admin_model->j_count($_param);

            $config['num_links'] = 6;
            $config['per_page'] = $limit;
            $config['use_page_numbers']=true;
            $_param['page'] = $page;
            $this->pagination->initialize($config);
            $data['pages'] = $this->pagination->create_links();

            //部门信息
            $model = get_model('department_model');
            $list = $model->j_list(['is_del' => 0]);
            $format_list = [];
            foreach($list as $value){
                $format_list[$value['id']] = $value['department_name'];
            }

            $data['group_list'] = $format_list;

            //角色信息
            $model = get_model('role_model');
            $role_list = $model->j_list(['is_del' => 0]);
            $role_format_list = [];
            foreach($role_list as $value){
                $role_format_list[$value['id']] = $value['role_name'];
            }
            $data['role_list'] = $role_format_list;

            $this->load->view('admin/list_admin', $data);
        }

        /**
         * 用户表单
         * author: walker
         * Date: 2019/11/8
         * Time: 10:48
         * Note:
         */
        public function user_from()
        {
            $this->load->model('admin_model');
            $model = new Admin_model();
            if ($_POST){

                $style_type = $this->input->post('style_type') ? : [];
                $data = array(
                    'admin_name' => $this->input->post('admin_name'),
                    'status'     => $this->input->post('status'),
                    'email'      => $this->input->post('email'),
                    'group'      => $this->input->post('group'),
                    'phone'      => $this->input->post('phone'),
                    'role_id'      => $this->input->post('role_id'),
                    'is_admin'      => $this->input->post('is_admin'),
                    'style_type'      => serialize($style_type),
                );
                $id   = $_POST['id'];
                if ( !empty($id)){
                    $data['admin_id'] = $_POST['id'];
                }
                /**
                 * 检查字段
                 */
                $this->check_field($data, array('admin_name' => '用户名称'));
                $role_info = $model->get_table_row('admin', array('admin_name' => $data['admin_name']));

                $data['update_time'] = time();

                if ($id){
                    $res                 = $model->update_row('admin', array('admin_id' => $id), $data);
                } else {
                    if ( !empty($role_info)){
                        ajaxReturn(4002, '该用户已经存在');
                    }
                    $data['add_time'] = time();
                    $data['password'] = md5('123456');
                    $res              = $model->add_row('admin', $data);
                }
                if ($res){
                    ajaxReturn(200, '添加成功', array('url' => 'department_list'));
                }
            }
            $id   = $_GET['id'];
            $info = array();
            if ( !empty($id)){
                $where             = array();
                $where['admin_id'] = $id;
                $info              = $this->admin_model->get_table_row('admin', $where);
            }

            $data             = array();

            $info['style_type'] = $info['style_type'] ? unserialize($info['style_type']) : [];
            $data['info']     = $info;
            $data['title']    = '用户';
            $data['form_url'] = 'user_from';

            //部门信息
            $model = get_model('department_model');
            $list = $model->j_list(['is_del' => 0]);
            $data['department']    = $list;

            //角色信息
            $model = get_model('role_model');
            $data['role'] = $model->j_list(['is_del' => 0]);

            $goods_model = get_model('goods_model');
            $data['style_setting'] = $goods_model->style_setting;

            $data['title']    = '用户';
            $this->load->view('admin/add_user', $data);
        }

        /**
         * 删除角色
         * author: walker
         * Date: 2019/11/7
         * Time: 15:41
         * Note:
         */
        public function sync_edit_user()
        {
            $this->load->model('admin_model');
            $params = $_POST;
            $id     = $params['id'];
            $act    = $params['act'];
            if (empty($id)){
                ajaxReturn(4001, '缺少必要参数');
            }
            $info = $this->admin_model->get_table_row('admin', array('admin_id' => $id));

            if (empty($info)){
                ajaxReturn(4002, '该消息不存在');
            }
            /**
             * 软删除
             */
            switch ($act) {
                case 'del':
                    $data = array('is_del' => 1, 'update_time' => time());
                    $res  = $this->admin_model->update_row('admin', array('admin_id' => $id), $data);
                    break;
                case 'status':
                    $res = $this->admin_model->edit_row_status('admin', array('admin_id' => $id));
                    break;
            }
            if ($res){
                ajaxReturn(200, '成功');
            }
            ajaxReturn(4002, '失败');
        }

    }
